package com.topera.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topera.commonutils.R;
import com.topera.eduservice.entity.EduCourse;
import com.topera.eduservice.entity.EduCourseDescription;
import com.topera.eduservice.entity.vo.CourseInfoVo;
import com.topera.eduservice.entity.vo.CoursePublish;
import com.topera.eduservice.service.EduChapterService;
import com.topera.eduservice.service.EduCourseDescriptionService;
import com.topera.eduservice.service.EduCourseService;
import com.topera.eduservice.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-05-18
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private EduVideoService videoService;


    /**
     * 添加课程信息
     * @return
     */
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String id= courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    /**
     * 根据id查询课程信息
     * @param courseId
     * @return
     */
    @GetMapping("getCourseById/{courseId}")
    public R getCourseById(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        //课程信息中 包括课程信息和课程简介
        EduCourse course = courseService.getById(courseId);
        //将课程信息 复制到 CourseInfoVo中
        BeanUtils.copyProperties(course,courseInfoVo);
        //查询课程简介信息
        EduCourseDescription description = courseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());

        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    /**
     * 更新课程信息
     * @param courseInfoVo
     * @return
     */
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        //课程信息中包括课程基本信息 和 简介  需要同时更新两张表的数据
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,course);
        courseService.updateById(course);

        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,description);
        courseDescriptionService.updateById(description);

        return R.ok();
    }

    /**
     * 获取发布课程的信息
     * @param courseId
     * @return
     */
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublish coursePublish = courseService.getPublishCourseInfo(courseId);
        return R.ok().data("coursePublish",coursePublish);
    }

    /**
     * 发布课程
     * @param id
     * @return
     */
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse course = new EduCourse();
        course.setId(id);
        course.setStatus("Normal");//已发布的状态
        courseService.updateById(course);
        return R.ok();
    }

    /**
     * 分页查询课程信息
     * @param current
     * @param limit
     * @param course
     * @return
     */
    @PostMapping("getCourseListPage/{current}/{limit}")
    public R getCourseListPage(@PathVariable long current, @PathVariable long limit,@RequestBody EduCourse course){

        //创建page对象
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        //构造查询条件
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if(course!=null){
            String title = course.getTitle();//课程标题
            BigDecimal price = course.getPrice();//课程价格
            String subjectParentId = course.getSubjectParentId();//一级分类id
            String subjectId = course.getSubjectId();//二级分类id
            String teacherId = course.getTeacherId();//讲师id
            if(!StringUtils.isEmpty(title)){
                queryWrapper.like("title",title);
            }
            if(!StringUtils.isEmpty(subjectParentId)){
                queryWrapper.eq("subject_parent_id",subjectParentId);
            }
            if(!StringUtils.isEmpty(subjectId)){
                queryWrapper.eq("subject_id",subjectId);
            }
            if(!StringUtils.isEmpty(teacherId)){
                queryWrapper.eq("teacher_id",teacherId);
            }
            if(price!=null){
                queryWrapper.gt("price",price);
            }
        }
        queryWrapper.orderByDesc("gmt_create");
        courseService.page(pageCourse,queryWrapper);

        long total = pageCourse.getTotal();//总记录数
        List<EduCourse> records = pageCourse.getRecords();//记录

        Map result = new HashMap();
        result.put("total",total);
        result.put("records",records);
        return R.ok().data(result);
    }

    /**
     * 删除课程
     * @param id
     * @return
     */
    @DeleteMapping("deleteCourse/{id}")
    public R deleteCourse(@PathVariable String id){

        //删除多个视频

        //删除课程描述信息
        courseDescriptionService.removeById(id);

        //删除章节信息
        chapterService.removeChapterByCourseId(id);

        //删除小节信息
        videoService.removeVideoByCourseId(id);

        courseService.removeById(id);
        return R.ok();
    }

}

